<?php

class Default_Model_Category extends Zend_Db_Table_Abstract
{
    protected $_name = 'category';
    public function getAllData(){
        $query = $this->select();
        $query->from($this->_name, array('categoryId', 'categoryCode', 'categoryName', 'note'));
        $query->order('categoryName ASC');
        
        return $this->fetchAll($query);
    }
    public function checkCategory($category)
    {
        $category = (int)($category);
        $query = $this->select()
                        ->setIntegrityCheck(false)
                        ->from(array('p' => $this->_name), array('p.categoryName'))
                        ->joinLeft(array('e'=>'equipment'), 'p.categoryId = e.categoryId',array())
                        ->where('e.categoryId = ?',$category)
                        ->group('p.categoryName');
                        
        return $this->fetchAll($query);
    }
    public function deleteData ($id)
    {
        $this->delete( 'categoryId = ' . (int) $id );
    }
    public function addData ($code, $name, $note = '', $createUser = 1)
    {
        $data = array ('categoryCode' => $code, 'categoryName' => $name, 'note' => $note, 'createUser' => $createUser, 'createDate' => date('Y-m-d H:i:s'));
        return $this->insert( $data );
    }
    public function getDataByID($id)
    {
        $id = (int) $id;
        $query = $this->select();
        $query->from($this->_name,array('categoryId', 'categoryCode', 'categoryName', 'note'));
        $query->where('categoryId = ?', $id);
        
        return $this->fetchRow($query);
    }
    public function updateData ($catId, $catCode, $catName, $note)
    {
        $data = array ( 'categoryCode' => $catCode,'categoryName' => $catName,'note' => $note );
        $this->update( $data, 'categoryId = ' . (int) $catId );
    }
    
}

























